package com.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.handler.RedisKeysHandler;
import com.user.domain.dto.RoleDto;
import com.user.domain.pojo.Permission;
import com.user.domain.pojo.Role;
import com.user.domain.pojo.RolePermission;
import com.user.domain.vo.RoleVo;
import com.user.mapper.RoleMapper;
import com.user.service.IRolePermissionService;
import com.user.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 小杰大人
 * @since 2025-02-27
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    IRolePermissionService iRolePermissionService;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public RoleVo getRoleDetail(Long id) {
        List<Permission> permissions =  baseMapper.querySelectPermissionByRoleId(id);
        Role role = this.getById(id);
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(role,roleVo);
        roleVo.setPermissions(permissions);
        return roleVo;
    }

    @Override
    @Transactional
    public void addRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        this.save(role);
        insertPermission(roleDto, role.getId());
    }

    @Override
    public void updateRole(RoleDto roleDto) {
        Role role = this.getById(roleDto.getId());
        // 清空当前角色有关的用户登入状态
        List<String> usernameList =  baseMapper.querySelectUsernameByRoleId(roleDto.getId()).stream().map(RedisKeysHandler::getUserAuthorityKey).toList();
        stringRedisTemplate.delete(usernameList);
        BeanUtils.copyProperties(roleDto,role);
        this.updateById(role);
        // 删除已有的权限
        iRolePermissionService.remove(new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getRoleId,roleDto.getId()));
        // 添加新的权限
        insertPermission(roleDto, role.getId());

    }

    @Override
    public void deleteRoles(List<Long> ids) {
        // 删除角色信息
        this.removeByIds(ids);
        for (Long id : ids) {
            // 清空当前角色有关的用户登入状态
            List<String> usernameList =  baseMapper.querySelectUsernameByRoleId(id).stream().map(RedisKeysHandler::getUserAuthorityKey).toList();
            stringRedisTemplate.delete(usernameList);
            // 删除已有的权限
            iRolePermissionService.remove(new LambdaQueryWrapper<RolePermission>()
                    .eq(RolePermission::getRoleId,id));
        }
    }

    private void insertPermission(RoleDto roleDto, Long roleId) {
        List<RolePermission> list = roleDto.getPermissionList().stream().map(permissionId -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            return rolePermission;
        }).toList();
        iRolePermissionService.saveBatch(list);
    }
}
